<template>
  <el-dialog
    title="修改用户"
    :visible.sync="this.$store.state.alterUser"
    width="50%"
    :lock-scroll="false"
    :before-close="alterClose"
    @close="alterUserClose"
  >
    <el-form :model="alterList" :rules="alterRules" ref="alterRef" label-width="70px">
      <el-form-item label="用户名">
        <el-input v-model="alterList.username" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="alterList.email"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="alterList.mobile"></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer">
      <el-button @click="closeClick">取 消</el-button>
      <el-button type="primary" @click="alterUser">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { putAlterUser } from "network/homelist";
export default {
  props: {
    alterList: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    var checkEmail = (rule, value, callback) => {
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
      if (regEmail.test(value)) {
        return callback();
      }
      callback(new Error("请输入合法邮箱"));
    };
    var checkTel = (rule, value, callback) => {
      const regTel = /^(0|86|17951)?(13[0-9]|15[0-9]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
      if (regTel.test(value)) {
        return callback();
      }
      callback(new Error("请输入合法手机号"));
    };
    return {
      alterRules: {
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "请输入电话", trigger: "blur" },
          { validator: checkTel, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    closeClick() {
      this.$store.commit("closeAlterUser");
    },
    alterUser() {
      this.$refs.alterRef.validate(valid => {
				if (!valid) return this.$Message.error("用户信息修改错误！");
				console.log(this.alterList)
        putAlterUser("/users/" + this.alterList.id,{email : this.alterList.email, mobile : this.alterList.mobile}).then(res => {
					console.log(res);
					
          if (res.data.meta.status !== 200) {
            this.$Message.error("更新用户信息失败！");
          } else {
						this.$emit("alterUser");
          }
        });
      });
    },
    alterClose() {
      this.$store.commit("closeAlterUser");
    },
    alterUserClose() {
      this.$refs.alterRef.resetFields();
    }
  }
};
</script>
<style lang="less" scoped>
</style>